package com.maiworld.api.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.maiworld.api.dto.CoachCommentUser;
import com.maiworld.api.vo.ResultVO;
import com.maiworld.pojo.TbReleaseNews;
import com.maiworld.seller.service.CoachService;

import uitl.HttpUtil;
import wxutils.Constant;

@RestController
@RequestMapping("/coach")
public class CoachController{
	
		@Reference
		private CoachService coachService;

		@RequestMapping("/findOne")
		public ResultVO<CoachCommentUser> findOne(String openid){
		return coachService.findCoachDetail(openid);
	}
		//当前驾校的所有科目二的教练，index:0:代表室内模拟练习 1：代表现场练习   (测试通过)
		@RequestMapping("/findSubject")
		public Map<String, Object> findSubject(String subject,String indoor,String status,String session_key){
			return coachService.findSubject(subject,indoor,status);
		}
		
		//教练登录(测试通过)
		@RequestMapping("/login")
		public Map<String, Object> login(String code,String nickname, String head_pic){
			Map<String, Object> result = new HashMap<String,Object>();
			try {
				nickname = new String((nickname).trim().getBytes("ISO-8859-1"), "UTF-8");
			} catch (UnsupportedEncodingException e) {				
				e.printStackTrace();
			}	    	   	
			String url ="https://api.weixin.qq.com/sns/jscode2session" + "?appid=" +Constant.coachappid+ "&secret=" +Constant.coachsecret+ "&js_code=" + code + "&grant_type="  
	                +"authorization_code";  
	        // 发送请求  
	        String data = HttpUtil.get(url);
	        //数据处理
	        JSONObject jsonobjectF = JSONObject.parseObject(data);
	        String sessionKey = jsonobjectF.getString("session_key");
	        String openid = jsonobjectF.getString("openid");	        
	       //System.out.print(q);
	        result = coachService.login(openid, nickname, head_pic);
	        result.put("session_key", sessionKey);
	        result.put("openinfo", data);
	        return result;	
		} 
		
		//修改教练信息(测试通过)
		@RequestMapping(value="updateCoachInfo",method=RequestMethod.POST)
		public Map<String, Object> updateCoachInfo(
				@RequestBody Map<String,String> params
				){
			return  coachService.updateCoach(params);
		}
		//提交认证信息       (测试通过)
		@RequestMapping(value = "/updaAuthentication",method=RequestMethod.POST)
		public Map<String, Object> updaAuthentication(
				@RequestBody Map<String, String> parmes
				){
			System.out.print(parmes);
			return coachService.updaAuthentication(parmes);
		}
		
		//获取 某个教练 某个日气的学员(测试通过)
		@RequestMapping("/mystudent")
		public Map<String, Object> mystudent(Long cid, String openid, String status, String date,String session_key){
			
			return coachService.mystudent(cid, openid, status, date);
		}
		
		//获取工单 已预约 已完成(测试通过)		
		@RequestMapping("/selectappointment")
		public Map<String , Object> selectappointment(Long cid, String openid, String status, String checkStatus,String date ,String session_key){
			return coachService.selectappointment(cid, openid, status, checkStatus,date);
		}
		
		//修改状态 确定为已完成 未完成(测试通过)
		@RequestMapping("/updateCheckStatus")
		public Map<String , Object> updateCheckStatus(Long reservationId, String status, String checkStatus, String openid,String session_key){
			
			return coachService.updateCheckStatus(reservationId, status, checkStatus, openid);
		
		}
}
